﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Diagnostics;
using System.ComponentModel;
using WPFExcelReport.Helpers;
using System.Windows.Input;
namespace WPFExcelReport
{
    public partial class TWT06
    {
        public TWT06()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWTEINZ();
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void LOADBYAUF()
        {

            if (!IsSaved)
                return;
            if (ORDERNUM.Length < 15)
                return;
            if (ORDERNUM != KEY)
                return;
            dwData = TBMain.NewRow();
            dwData["WTEMANDANT"] = app.Mandant;
            dwData["WTEEINZNR"] = " AND WTEEINZNR='" + ORDERNUM.Substring(0, 15) + "'";
            SResult = TMV.QWTEINZ03(dwData);
            TBTemp = TBMain.Clone();
            TBTemp.Merge(SResult.Tables[0]);
            foreach (DataRow row in TBTemp.Rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            }
            IsSaved = false;

        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            dwData["WTEMANDANT"] = app.Mandant;
            dwData["WTEEINZNR"] = " AND WTEEINZNR LIKE '%" + ART.Trim() + "%'";
            SResult = TMV.QWTEINZ03(dwData);
            MergeData(SResult, true);
            base.QueryCommand(sender, e);
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                IsNew = true;
                ART = "";
                return;
            } 
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["WTEMANDANT"] = app.Mandant;
            dwData["WTEEINZNR"] = ART.Trim().PadRight(15);
            TBMain.Rows.InsertAt(dwData, 0);
            TMV.InsertWTEINZ(dwData);
            vwMaster.View.MoveCurrentToFirst();
            IsNew = false;
            ART = "";
            base.NewCommand(sender, e);
        }
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            if (!IsCopy)
            {
                IsCopy = true;
                ART = "";
                return;
            }
           
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray; 
            dwData["WTEEINZNR"] = ART.Trim().PadRight(15);
            TBMain.Rows.InsertAt(dwData, 0);
            TMV.InsertWTEINZ(dwData);
            vwMaster.View.MoveCurrentToFirst();
            IsCopy = false;
            ART = "";
            base.CopyCommand(sender, e);
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DWTE01(dwData);
            filter = "WTEEINZNR='" + dwMaster["WTEEINZNR"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach(DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }
        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            base.EditCommand(sender, e);
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            base.SaveCommand(sender, e);
        }
        public override void OnEEZChg()
        {
            if (dwMaster == null)
                return;
            TMV.WTEEZ = EEZ;
            base.OnEEZChg();
        }
       
    }
}
